package ubi.quando.UI;

import ubi.quando.App.ContactWallet;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends Activity {

	private UbiApplication mApp;

	private Button mBtnRegister;
	private Button mBtnCancel;
	private EditText mTxtMail;
	private EditText mTxtPwd;
	private EditText mTxtName;
	private TextView mMail;
	private TextView mPassword;
	private TextView mName;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);

		mApp = (UbiApplication) getApplication();
		mApp.setCurrentActivity(this);

		mBtnRegister = (Button) findViewById(R.id.btn_register);
		mBtnCancel = (Button) findViewById(R.id.btn_cancel);
		mTxtMail = (EditText) findViewById(R.id.txt_mail);
		mTxtPwd = (EditText) findViewById(R.id.txt_pwd);
		mTxtName = (EditText) findViewById(R.id.txt_name);
		mMail = (TextView) findViewById(R.id.mail);
		mPassword = (TextView) findViewById(R.id.password);
		mName = (TextView) findViewById(R.id.name);
		initFont();

		mBtnCancel.setOnClickListener(new CancelOnClickListener());
	}
	
	private void initFont() {
		Typeface font = Typeface.createFromAsset(getAssets(), "AGENCYR.TTF");  
		mName.setTypeface(font);
		mPassword.setTypeface(font);
		mMail.setTypeface(font);
		mBtnRegister.setTypeface(font);
		mBtnCancel.setTypeface(font);
		mTxtMail.setTypeface(font);
		mTxtPwd.setTypeface(font);
		mTxtName.setTypeface(font);
	}

	public class CancelOnClickListener implements OnClickListener {
		public void onClick(View v) {
			Intent login = new Intent(v.getContext(), LoginActivity.class);

			startActivity(login);
		}
	}

	public void onRegister(View view) {
		Log.e("Super", "Starting registration");
		Toast.makeText(this, "Starting", Toast.LENGTH_LONG).show();

		String mail = mTxtMail.getText().toString();
		String pwd = mTxtPwd.getText().toString();
		String name = mTxtName.getText().toString();
		
		mApp.setMail(mail);
		mApp.setPwd(pwd);
		mApp.getServerComm().register(mail, pwd, name,
				(UbiApplication) getApplication());
	}
	
	public void onRegistered(ContactWallet cw)
	{
		if(mApp.setUser(cw))
		{
			Intent mainMenu = new Intent(getApplicationContext(), MainMenu.class);

			startActivity(mainMenu); 
		}
		else {
			Toast.makeText(getApplicationContext(),
					"Mail or password are not recognized", Toast.LENGTH_SHORT)
					.show();
		}
	}

}
